//
//  RecommendViewController.swift
//  HLSwiftWork
//
//  Created by hulinhu on 2018/6/15.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit
import Alamofire


fileprivate let categoryCellID = "categoryCellID"
fileprivate let userCellID = "userCellID"

class RecommendViewController: HLBaseViewController {
    
    // 初始化 tableView
    lazy var categoryTableView : UITableView = {
        // self 必须加上
        let sframe = CGRect(x: 0, y: kTopHeight, width: 65 , height: kScreenHeight-kTopHeight)
        let tabView = UITableView(frame:sframe , style:.plain)
        tabView.delegate = self
        tabView.dataSource = self
        tabView.separatorStyle = .none
//        tabView.tableFooterView = UIView.()
        tabView.showsVerticalScrollIndicator = false
        return tabView
        
    }()
    
    // 初始化 tableView
    lazy var userTableView : UITableView = {
        // self 必须加上
        let sframe = CGRect(x: 65, y: kTopHeight, width: self.view.zc_width-65 , height: kScreenHeight-kTopHeight)
        let tabView = UITableView(frame:sframe , style:.plain)
        tabView.delegate = self
        tabView.dataSource = self
        tabView.separatorStyle = .none
//        tabView.tableFooterView = UIView()
        tabView.showsVerticalScrollIndicator = false
        return tabView
        
    }()

    lazy var recommendVM = RecommendViewModel()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
        recommendVM.getCategoryData { [weak self] in
            print("日了够了---------------------")
            self!.categoryTableView.reloadData()
            self!.categoryTableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .top)
            guard let categoryModel = self?.recommendVM.categoryArray.first  else {return}
            self?.recommendVM.getUserData(catagroy:categoryModel, jsonDataBlock: {
                self!.userTableView.reloadData()
            })
        }
    }

}
extension RecommendViewController {
    
    func setupUI() {
        view.addSubview(categoryTableView)
        view.addSubview(userTableView)
        
        navigationItem.title = "推荐关注"
        categoryTableView.scrollsToTop = false
        categoryTableView.register(UINib.init(nibName: "RecommendCategoryCell", bundle: nil), forCellReuseIdentifier: categoryCellID)
        userTableView.register(UINib.init(nibName: "RecommendUserCell", bundle: nil), forCellReuseIdentifier: userCellID)
    }
}

extension RecommendViewController:UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if tableView == categoryTableView {
            return 40
        }else {
            return 70
        }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == categoryTableView {
            let row = categoryTableView.indexPathForSelectedRow?.row
            let model = recommendVM.categoryArray[row!]
            recommendVM.getUserData(catagroy: model, jsonDataBlock: {
                self.userTableView.reloadData()
                self.userTableView.scrollToRow(at:  IndexPath(row: 0, section: 0), at: .top, animated: false)
            })
        }else {
            
        }
    }
}

extension RecommendViewController:UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == categoryTableView {
            return recommendVM.categoryArray.count
        }else {
            guard let row = categoryTableView.indexPathForSelectedRow?.row else {
                return 0
            }
            return recommendVM.categoryArray[row].users.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == categoryTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: categoryCellID) as! RecommendCategoryCell
            cell.model = recommendVM.categoryArray[indexPath.row]
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: userCellID) as! RecommendUserCell
            let row = categoryTableView.indexPathForSelectedRow?.row
            let category = recommendVM.categoryArray[row!]
            let array = category.users
            if array.count > 0 {
                cell.model = array[indexPath.row]
            }
            //在这里校验 是否还能再继续加载避免再次发起请求
            if indexPath.row == category.users.count - 1 && category.canLoadMore{
                recommendVM.getUserData(catagroy: recommendVM.categoryArray[row!], jsonDataBlock: {
                    self.userTableView.reloadData()
                })
            }
            return cell
        }
    }
}







